package org.erepublik.company.charts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.erepublik.company.EAlliance;
import org.erepublik.company.EWorld;
import org.erepublik.company.Location;
import org.erepublik.company.comparators.ProductComparatorByPriceInGold;
import org.erepublik.company.product.Ticket;
import org.erepublik.company.searchCriteria.EMovingDistance;
import org.erepublik.company.service.IVatService;
import org.erepublik.company.service.JSONTicketService;
import org.erepublik.company.service.XmlVatService;
import org.json.JSONArray;
import org.json.JSONException;

public class TicketJSONDataCollector extends AbstractJSONDataCollector<Ticket> {

    public TicketJSONDataCollector(IVatService vatService) {
        super("ticket", EMovingDistance.class, vatService);
    }

    @Override
    protected ArrayList<Ticket> getProductFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        return JSONTicketService.getTicketFromJSONArray(jsonArrayWithFood);
    }

    final int maxOffers=4;
    final int ticketLimit=50;
    @Override
    @SuppressWarnings("unchecked")
    protected double calculateOffers(Ticket[] bestOffers, Enum criteria) {

        double attributeSum = 0.0d;
        double valueSum = 0.0d;
        for (Ticket food : bestOffers) {
            valueSum += food.getPriceInGold() * food.getQuantity();
            attributeSum += food.getQuantity();
            if (attributeSum>ticketLimit)
                break;
        }
        if (valueSum==0)
            return 0.0d;
     
        return valueSum/ attributeSum;
    }

    @Override
    @SuppressWarnings("unchecked")
    protected Ticket[] getNBestOffers(int offers, ArrayList<Ticket> food, Enum criteria, Location country) {
        EMovingDistance comparator = (EMovingDistance) criteria;
        Ticket[] foodArray = filter(food, country, comparator).toArray(new Ticket[0]);

        Arrays.sort(foodArray, new ProductComparatorByPriceInGold());
        Ticket[] shorArray = new Ticket[Math.min(offers, foodArray.length)];
        for (int i = 0; i < shorArray.length; i++) {
            shorArray[i] = foodArray[i];
        }
        return shorArray;
    }

    private ArrayList<Ticket> filter(ArrayList<Ticket> food, Location country, EMovingDistance comparator) {

        if (country instanceof EAlliance) {
            ArrayList<Ticket> filteredData = new ArrayList<Ticket>();
            for (Ticket food2 : food) {
                if (food2.getCountry().getAlliance() == country && food2.getDistance() == comparator.getDistance())
                    filteredData.add(food2);
            }
            return filteredData;
        }
        
        ArrayList<Ticket> filteredData = new ArrayList<Ticket>();
        for (Ticket food2 : food) {
            if ((country == EWorld.GLOBAL || food2.getCountry() == country) && food2.getDistance() == comparator.getDistance())
                filteredData.add(food2);
        }
        return filteredData;
    }
    
  
    
    public static void main(String[] args) {
        TicketJSONDataCollector dataCollector = new TicketJSONDataCollector(new XmlVatService());
        dataCollector.updateData();
        ChartDataSet dataset = dataCollector.getData(EAlliance.PHOENIX, EMovingDistance.ZONE2.toString());
        List<ChartData> data = dataset.getData();
        for (ChartData chartData : data) {
            System.out.println( new Date(chartData.getTime()) + " "+chartData.getValue());
            
        }
    }
    
    
    
    

}
